//豆瓣电影数据爬取puppeteer和cheerio
const url = 'https://movie.douban.com/tag/#/?sort=T&range=7,10&tags='
const puppeteer = require('puppeteer')
const cheerio = require('cheerio')

;(async () => {
  console.log(`开始了`);

  const browser = await puppeteer.launch()
  const page = await browser.newPage();
  await page.goto(url, {
    waitUntil: 'networkidle2'
  })

  
  //等待多少秒
   await page.waitFor(1000)
 

  await page.waitForSelector('.more')
  for (let i = 0; i < 1; i++) {
    await page.waitFor(500);
    await page.click('.more')
  }
  //page.content()得到整个网页,然后把content给cheerio第三方库(类似jquery)从而解析
  const content = await page.content();
  const $ = cheerio.load(content, { decodeEntities: false });

  const result = await page.evaluate(() => { 
    var items =  $('.list-wp a')
    var links = []
    if (items.length >= 1) {
      items.each((index, item) => {
        let it = $(item);
        let doubanId = it.find('div').data('id')
        let title = it.find('.title').text()
        let rate = Number(it.find('.rate').text())
        let poster = it.find('img').attr('src').replace('s_ratio', 'l_ratio')
        links.push({
          doubanId,
          title,
          rate,
          poster
        })
      })
    }
    return links
  })

  browser.close();
  console.log(result);

})()